<template>
  <div>
    <el-select placeholder="请选择" v-model="brandId" filterable clearable>
      <el-option v-for="item in brands" :key="item.brandId" :label="item.brandName" :value="item.brandId"></el-option>
    </el-select>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import PubSub from 'pubsub-js';

export default {
  components: {},
  props: {},
  data() {
    return {
      catId: 0,
      brands: [
        { label: "a", value: 1 }
      ],
      brandId: "",
      subscribe: null
    };
  },
  computed: {},
  watch: {
    brandId(val) {
      PubSub.publish("brandId", val); // 使用 PubSub.publish 发布消息
    }
  },
  methods: {
    getCatBrands() {
      // 假设你已经在 Vue 实例中配置了 $http 插件
      this.$http({
        url: this.$http.adornUrl("/product/categorybrandrelation/brands/list"),
        method: "get",
        params: this.$http.adornParams({
          catId: this.catId
        })
      }).then(({ data }) => {
        this.brands = data.data; // 更新 brands 数据
      }).catch(error => {
        console.error("Failed to fetch brands:", error);
      });
    }
  },
  created() { },
  mounted() {
    // 监听三级分类消息的变化
    this.subscribe = PubSub.subscribe("catPath", (msg, val) => {
      this.catId = val[val.length - 1];
      this.getCatBrands();
    });
  },
  beforeCreate() { },
  beforeMount() { },
  beforeUpdate() { },
  updated() { },
  beforeDestroy() {
    PubSub.unsubscribe(this.subscribe); // 取消订阅以防止内存泄漏
  },
  destroyed() { },
  activated() { }
};
</script>
<style scoped>
</style>